package com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entity.Goods;
import com.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;


    @RequestMapping("/")
    public String index() {
        return "goods-index";
    }


    /**
     * 查询全部
     *
     * @param request
     * @return String
     */
    @RequestMapping("selectPage")
    public String selectPage(@RequestParam(value = "current", defaultValue = "1") Long current, @RequestParam(value = "size", defaultValue = "10") Long size, HttpServletRequest request) {
        Page<Goods> goodsPage = new Page<>(current, size);
        Page<Goods> selectPage = goodsService.selectPage(goodsPage);
        request.setAttribute("goodsList", selectPage.getRecords());
        request.setAttribute("pages", selectPage.getPages());
        request.setAttribute("current", selectPage.getCurrent());
        request.setAttribute("total", selectPage.getTotal());
        return "goods-list";
    }

    @RequestMapping("save")
    public String save(Goods goods) {
        goods.setCreateTime(new Date());
        goodsService.save(goods);
        return "redirect:/goods/findAll";
    }


    @RequestMapping("getUpdateDetail")
    public String getUpdateDetail(Integer id, HttpServletRequest request) {
        Goods goods = goodsService.findById(id);
        request.setAttribute("goods", goods);
        return "goods-update";
    }

    @RequestMapping("getAddDetail")
    public String getAddDetail(Integer id, HttpServletRequest request) {
        return "goods-add";
    }



    @RequestMapping("update")
    public String updateDetail(Goods goods) throws IOException {
        goodsService.update(goods);
        return "redirect:/employee/findAll";
    }


    @RequestMapping("delete")
    public String delete(Integer id) {
        goodsService.delete(id);
        return "redirect:/employee/findAll";
    }
}
